package com.jy.rhin.utils;

import com.jy.common.StringUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class CDAUtil {


    public static String toD8(Date date) {
        if (date == null) return defaultD8();
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
        return s.format(date);
    }

    public static String defaultD8() {
        return "19990101";
    }
    public static String defaultD15() {
        return "19990101000000";
    }

    public static String defaultMobile() {
        return "13700000000";
    }

    /**
     * 转化为17位数
     *
     * @param number
     * @return
     */
    public static String formatNumber(String number) {
        if (StringUtil.isNullOrEmpty(number)) {
            return "10000000000000000"; // 如果为空，补全17
        }
        int len = number.length();
        if (len < 17) {
            StringBuilder sb = new StringBuilder(number);
            for (int i = 0; i < 17 - len; i++) {
                sb.append("0"); // 不足17位，则在后面补0
            }
            return sb.toString();
        } else if (len > 17) {
            return number.substring(0, 17); // 超过17位，则截取前17位
        } else {
            return number; // 等于17位，直接返回
        }
    }

    public static String toD15(Date date) {
        if (date == null) return null;
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss");
        return s.format(date);
    }

    public static String toJGID10(String hospitalId) {
        String resp = "";
        if (StringUtil.isNullOrEmpty(hospitalId)) {
            resp = "1000000000";
        } else if (hospitalId.length() > 10) {
            resp = hospitalId.substring(hospitalId.length() - 10);
        } else if (hospitalId.length() < 10) {
            resp = StringUtil.padLeft(hospitalId, 10, '0');
        } else {
            resp = hospitalId;
        }
        return resp;
    }

    public static String toD8(LocalDateTime date) {
        if (date == null) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return formatter.format(date);
    }

    public static String toD15(LocalDateTime date) {
        if (date == null) {
            return null;
        }
        DateTimeFormatter s = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return s.format(date);
    }

    public static void main(String[] args) {
        System.out.println(formatNumber("1222353434645664656"));
    }
}
